package com.longshihan.carservice.controller;

import com.github.pagehelper.PageInfo;
import com.longshihan.carservice.http.APIResponse;
import com.longshihan.carservice.model.NoticeDomain;
import com.longshihan.carservice.service.notice.NoticeService;
import com.longshihan.carservice.utils.DateKit;
import com.longshihan.carservice.utils.GsonUtils;
import com.longshihan.carservice.utils.UUID;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Api("公告相关")
@Controller
@RequestMapping("/notice")
public class NoticeController extends BaseController{

    @Autowired
    private NoticeService noticeService;

    @ApiOperation(value = "添加公告",notes = "json转义添加")
    @PostMapping(value = "/addNotice")
    @ResponseBody
    public APIResponse saveNotice(
            HttpServletRequest request, HttpServletResponse response,
            @ApiParam(name = "notice", value = "公告", required = true) @RequestParam(name = "notice", required = true) String notice,
            @ApiParam(name = "userID", value = "用户ID", required = true) @RequestParam(name = "userID", required = true) String userID

    ){
        try {
            NoticeDomain domain= (NoticeDomain) GsonUtils.JSONToObject(notice,NoticeDomain.class);
            if (domain==null|| TextUtils.isBlank(userID)){
               return APIResponse.fail("序列化失败");
            }else {
                logger.info(UUID.UU32());
                domain.setId(UUID.UU32());
                domain.setUserId(userID);
                if (1==noticeService.addNotice(domain)){
                    return APIResponse.success();
                }else {
                    return APIResponse.fail("添加公告失败");
                }
            }
        }catch (Exception e){
            return APIResponse.fail("添加公告失败:"+e.getMessage());
        }
    }

    @ApiOperation(value = "删除公告")
    @PostMapping(value = "/delNotice")
    @ResponseBody
    public APIResponse delNotice(
            HttpServletRequest request, HttpServletResponse response,
            @ApiParam(name = "noticeId", value = "公告ID", required = true) @RequestParam(name = "noticeId", required = true) String noticeId,
            @ApiParam(name = "userID", value = "用户ID") @RequestParam(name = "userID") String userID

    ){
        try {
            if (TextUtils.isBlank(noticeId)){
                return APIResponse.fail("公告ID为空");
            }else {
                if (noticeService.delNotice(noticeId)==1){
                    return APIResponse.success();
                }else {
                    return APIResponse.fail("删除公告失败");
                }
            }
        }catch (Exception e){
            return APIResponse.fail("删除公告失败:"+e.getMessage());
        }
    }


    @ApiOperation(value = "获取公告")
    @PostMapping(value = "/getNoticeById")
    @ResponseBody
    public APIResponse getNoticeById(
            HttpServletRequest request, HttpServletResponse response,
            @ApiParam(name = "noticeId", value = "公告ID", required = true) @RequestParam(name = "noticeId", required = true) String noticeId,
            @ApiParam(name = "userID", value = "用户ID") @RequestParam(name = "userID") String userID

    ){
        try {
            if (TextUtils.isBlank(noticeId)){
                return APIResponse.fail("公告ID为空");
            }else {
                NoticeDomain domain=noticeService.getNoticeById(noticeId);
                if (domain!=null){
                    return APIResponse.success(GsonUtils.toJsonString(domain));
                }else {
                    return APIResponse.fail("获取公告失败");
                }
            }
        }catch (Exception e){
            return APIResponse.fail("获取公告失败:"+e.getMessage());
        }
    }


    @ApiOperation(value = "获取公告")
    @PostMapping(value = "/getNoticeList")
    @ResponseBody
    public APIResponse getNoticeList(
            HttpServletRequest request, HttpServletResponse response,
            @ApiParam(name = "userID", value = "用户ID") @RequestParam(name = "userID") String userID,
            @ApiParam(name = "customerId", value = "商家ID") @RequestParam(name = "customerId") String customerId,
            @ApiParam(name = "pageNum", value = "页面数",defaultValue = "1") @RequestParam(name = "pageNum",defaultValue = "1") int pageNum,
            @ApiParam(name = "pagesize", value = "单次查询的个数", defaultValue = "20") @RequestParam(name = "pagesize", defaultValue = "20") int pagesize

    ){
        try {
            if (TextUtils.isBlank(customerId)){
                return APIResponse.fail("商家ID为空");
            }else {
                PageInfo<NoticeDomain> domains=noticeService.getNotices(customerId,pageNum,pagesize);
                if (domains!=null){
                    return APIResponse.success(GsonUtils.toJsonString(domains));
                }else {
                    return APIResponse.fail("获取公告列表失败");
                }
            }
        }catch (Exception e){
            return APIResponse.fail("获取公告列表失败:"+e.getMessage());
        }
    }

}
